const byte irPin=2;
const int MAXPULSO = 65000;
const byte RESOLUCION = 20;
word pulsos[100][2]; 
byte pulsoactual = 0; 

setup(){
	Serial.begin(9600);
}

loop(){
      word contadortiempohigh =0;
      word contadortiempolow  =0;
      while ((PIND & _BV(irPin))) {  
	    contadortiempohigh++;
	    delayMicroseconds(RESOLUCION);
	    if ((contadortiempohigh >= MAXPULSO) && (pulsoactual != 0)) {
		printpulsos();
		pulsoactual=0;
		return;
	    }
      }
      pulsos[pulsoactual][0] = contadortiempohigh;
      while (!(PIND & _BV(irPin))) { 
	    contadortiempolow++;
	    delayMicroseconds(RESOLUCION);
	    if ((contadortiempolow >= MAXPULSO) && (pulsoactual != 0)) {
		printpulsos();
		pulsoactual=0;
		return;
	    }
      }
      pulsos[pulsoactual][1] = contadortiempolow;
      pulsoactual++;
}

void printpulsos() {
      byte i;
      Serial.println("OFF \tON");
      for (i = 0; i < pulsoactual; i++) {
	 Serial.print(pulsos[i][0] * RESOLUCION);
	 Serial.print(" usec, ");
	 Serial.print(pulsos[i][1] * RESOLUCION);
	 Serial.println(" usec");
      }
}


